package testItem;

import com.yy.untils.MybatisUntil;
import com.yy.pojo.User;
import org.apache.ibatis.session.SqlSession;
import org.junit.Test;

import java.io.IOException;
import java.util.List;

public class usertest {
    @Test
    //   查所有
    public void t1() throws IOException {
        SqlSession session = MybatisUntil.getSession();
        List<User> selectUser = session.selectList("selectUser");
        for (User user : selectUser) {
            System.out.println(user);
        }
    }

    @Test
    //  查一个
    public void t2() throws IOException {
        SqlSession session = MybatisUntil.getSession();
        Object o = session.selectOne("selectUserById", "admin");
        System.out.println(o);
    }

    @Test
    //  新增
    public void t3() throws IOException {
        SqlSession session = MybatisUntil.getSession();
        User user = new User(0, "yy@163.com", "112233");
        int i = session.insert("addUser", user);
        if (i >= 1) {
            System.out.println("添加成功");
            session.commit();
        } else {
            System.out.println("添加失败");
        }
    }

    //  修改
    @Test
    public void t4() throws IOException {
        SqlSession session = MybatisUntil.getSession();
        User user = new User(3, "yy", "000000");
        int i = session.update("updateUserById", user);
        if (i >= 1) {
            System.out.println("修改成功");
            session.commit();
        } else {
            System.out.println("修改失败");
        }
    }

    //  删除
    @Test
    public void t5() throws IOException {
        SqlSession session = MybatisUntil.getSession();
        int i = session.delete("deleteUserByName", "yy");
        if (i >= 1) {
            System.out.println("删除成功");
            session.commit();
        } else {
            System.out.println("删除失败");
        }
    }
}
